package com.bangbang.config;

import com.bangbang.mapper.TbMemberMapper;
import com.bangbang.mapper.TbStudentUserMapper;
import com.bangbang.pojo.TbMember;
import com.bangbang.pojo.TbStudentUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configurable
@EnableScheduling
@Component
public class DingShi {

    @Autowired
    private TbMemberMapper tbMemberMapper;


    @Autowired
    private TbStudentUserMapper tbStudentUserMapper;



    //             定时执行任务
//  删

    @Scheduled(cron = "0 0 8 * * ? " )
    public void sendMerchantEmail (){
//       查找剩余时间=0的会员
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<TbMember> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("member_residueTime", 0 );
        List<TbMember> tbMembers = tbMemberMapper.selectList(memberQueryWrapper);
        for (TbMember tbMember : tbMembers) {
            Long memberUserId = tbMember.getMemberUserId();
            Long memberId = tbMember.getMemberId();
            tbMemberMapper.deleteById(memberId);
            Map<String, Object> map1 = new HashMap<>();
            QueryWrapper<TbStudentUser> studentUserQueryWrapper = new QueryWrapper<>();
            studentUserQueryWrapper.eq("user_id", memberUserId);
            List<TbStudentUser> tbStudentUsers = tbStudentUserMapper.selectList(studentUserQueryWrapper);
            for (TbStudentUser tbStudentUser : tbStudentUsers) {
                TbStudentUser tbStudentUser1 = new TbStudentUser();
                tbStudentUser1.setUserMemberCreateTime(new Date(0)).setUserWhetherMember(0).setUserMemberOverTime(new Date(0));
                Map<String, Object> map2 = new HashMap<>();
                QueryWrapper<TbStudentUser> studentUserQueryWrapper1 = new QueryWrapper<>();
                studentUserQueryWrapper1.eq("user_id", memberUserId);
                tbStudentUserMapper.update(tbStudentUser1,studentUserQueryWrapper1);
            }
        }
    }

    @Scheduled(cron = "0 0 2 * * ?" )  // 定时更新剩余时间
    public void UpdateRemainingTime (){

        List<TbMember> tbMembers = tbMemberMapper.selectList(null);


        for (TbMember tbMember : tbMembers) {

            Date date = new Date(); //当前时间
            Date memberOvertime = tbMember.getMemberOvertime();//结束时间
//        ----------------------计算剩余时间
            long d = (memberOvertime.getTime() - date.getTime());
            double day = Math.floor((d / (24 * 60 * 60 * 1000)));
            int days = (int) day;
            Long memberId = tbMember.getMemberId();
            String Residuetime = String.valueOf(days);
            TbMember tbMember1 = new TbMember();
            tbMember1.setMemberId(memberId).setMemberResiduetime(Residuetime);
            tbMemberMapper.updateById(tbMember1);
        }
    }

}
